/**
    搭建 HTTP 服务.
    GET  /table  响应一个表格 4 行 3 列表格, 并实现隔行换色 (JS)
 */
const data = [
    {
        id:1,
        name: 'gem',
        song: '野狼disco'
    },
    {
        id:2,
        name: '周杰伦',
        song: '不能说的密码'
    },
    {
        id:3,
        name:'林俊杰',
        song: '不为谁而作的歌'
    },
    {
        id:4,
        name: '五月天',
        song:'干杯'
    },
    {
        id: 5,
        name: '张艺兴',
        song: '莲'
    },
    {
        id:6,
        name:'刘德华',
        song:'冰雨'
    },
    {
        id: 7,
        name: '张学友',
        song: '情人'
    }
];

require('http').createServer((request, response) => {
    let body = '';
    body += `<!doctype html>
    <html>
        <head>
            <title>歌曲列表</title>
            <meta charset="utf-8" />
            <style>
                table,td{
                    border-collapse: collapse;
                }
                td{
                    padding:20px 10px;
                }
            </style>
        </head>
        <body>
            <table border="1">
                <tr><td>ID</td><td>歌手名称</td><td>歌曲</td></tr>`;
    //根据数组的数据, 拼接 tr 标签内容
    data.forEach(item => {
        body += `<tr><td>${item.id}</td><td>${item.name}</td><td>${item.song}</td></tr>`;
    });

    body += `</table>
                </body>
            </html>`;
    //响应结果
    response.end(body);
}).listen(80);

